package com.gcit.seniorWish.web.modular.importAdmission.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gcit.seniorWish.web.common.persistence.dao.AdmissionInfoMapper;
import com.gcit.seniorWish.web.common.persistence.model.AdmissionInfo;
import com.gcit.seniorWish.web.common.persistence.model.ExamineeScore;
import com.gcit.seniorWish.web.core.shiro.ShiroUser;
import com.gcit.seniorWish.web.modular.importAdmission.service.IAdmissionInfoService;
import com.gcit.seniorWish.web.modular.importAdmission.transfer.AdmissionDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class AdmissionInfoServiceImpl extends ServiceImpl<AdmissionInfoMapper, AdmissionInfo>
		implements IAdmissionInfoService {
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	@Transactional
	public String importAdmission(InputStream inputStream,ShiroUser shiroUser) {
		StringBuffer msg = new StringBuffer();
		ExcelReader reader = ExcelUtil.getReader(inputStream);
		Map<String, String> headerAlias = new HashMap<String, String>();
		headerAlias.put("准考证号", "examCardNumber");
		headerAlias.put("考生姓名", "studentName");
		headerAlias.put("考场号", "examSiteNumber");
		headerAlias.put("座位号", "seatNumber");
		headerAlias.put("建档号", "archivesNumber");
		headerAlias.put("录取学校代码", "enterSchoolCode");
		headerAlias.put("录取学校名称", "enterSchoolName");
		headerAlias.put("录取专业代码", "enterClassCode");
		headerAlias.put("录取专业名称", "enterClassName");
		reader.setHeaderAlias(headerAlias);
		List<AdmissionDto> excelData = reader.readAll(AdmissionDto.class);

		if (excelData.size() > 0) {
			Iterator excelIt = excelData.iterator();
			AdmissionDto item = null;
			int i = 0;
			// 循环列表中的记录
			while (excelIt.hasNext()) {
				i++;
				item = (AdmissionDto) excelIt.next();
				// 判断必填内容是否填写

				if (StrUtil.isBlank(item.getExamCardNumber())) {
					msg.append("第" + i + "条缺少准考证编号\n");
					continue;
				} else if(baseMapper.selectCount(Condition.create().eq("exam_card_number",item.getExamCardNumber()))> 0){
					msg.append("第" + i + "条准考证已经存在\n");
					continue;
				}
				if (StrUtil.isBlank(item.getStudentName())) {
					msg.append("第" + i + "条缺少考生姓名\n");
					continue;
				}

			}
			if (StrUtil.isBlank(msg.toString())) {
				// 数据保存
				for (AdmissionDto es : excelData) {
					AdmissionInfo admissionInfo = new AdmissionInfo();

					admissionInfo.setArchivesNumber(es.getArchivesNumber());
					admissionInfo.setEnterClassCode(es.getEnterClassCode());
					admissionInfo.setEnterClassName(es.getEnterClassName());
					admissionInfo.setEnterSchoolCode(es.getEnterSchoolCode());
					admissionInfo.setEnterSchoolName(es.getEnterSchoolName());
					admissionInfo.setExamCardNumber(es.getExamCardNumber());
					admissionInfo.setExamSiteNumber(es.getExamSiteNumber());
					admissionInfo.setSeatNumber(es.getSeatNumber());
					admissionInfo.setStudentName(es.getStudentName());
					baseMapper.insert(admissionInfo);
				}
			}
		} else {
			msg.append("导入数据有误，请检查\n");
		}
		return msg.toString();
	}
}
